#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
/*  新版LED驱动，有设备树，自动加载设备节点，使用GPIO、pinctrl子系统，
*   基于platform虚拟总线，platform设备使用设备树描述
*   使用OF函数读取设备树相关属性
*/

#define LED_CNT				1		  		/* 设备号个数 */
#define LED_NAME		"smartled"	/* 名字 */
#define LEDOFF 				0					/* 关灯 */
#define LEDON 				1					/* 开灯 */

/* smartled设备结构体 */
struct smartled_dev{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;	/* 类 		*/
	struct device *device;	/* 设备 	 */
	int major;				/* 主设备号	  */
	int minor;				/* 次设备号   */
	struct device_node	*nd; /* 设备节点 */
	int led_gpio;			/* led所使用的GPIO编号		*/
};

struct smartled_dev smartled;	/* led设备 */

static int led_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &smartled; /* 设置私有数据 */
	return 0;
}

static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;
	struct smartled_dev *dev = filp->private_data;

	retvalue = copy_from_user(databuf, buf, cnt);

	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		gpio_set_value(dev->led_gpio, 0);	/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		gpio_set_value(dev->led_gpio, 1);	/* 关闭LED灯 */
	}
	return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/* 设备操作函数 */
static struct file_operations smartled_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
	.release = 	led_release,
};
static int led_probe(struct platform_device *dev)
{
    int ret;
		/* 注册字符设备驱动 */
	/* 1、创建设备号 */
	if (smartled.major) {		/*  定义了设备号 */
		smartled.devid = MKDEV(smartled.major, 0);
		register_chrdev_region(smartled.devid, LED_CNT, LED_NAME);
	} 
    else {						/* 没有定义设备号 */
		alloc_chrdev_region(&smartled.devid, 0, LED_CNT, LED_NAME);	/* 申请设备号 */
		smartled.major = MAJOR(smartled.devid);	/* 获取分配号的主设备号 */
		smartled.minor = MINOR(smartled.devid);	/* 获取分配号的次设备号 */
	}
	printk("smartled major=%d,minor=%d\r\n",smartled.major, smartled.minor);	
	
	/* 2、初始化cdev */
	smartled.cdev.owner = THIS_MODULE;
	cdev_init(&smartled.cdev, &smartled_fops);
	
	/* 3、添加一个cdev */
	cdev_add(&smartled.cdev, smartled.devid, LED_CNT);

	/* 4、创建类 */
	smartled.class = class_create(THIS_MODULE, LED_NAME);
	if (IS_ERR(smartled.class)) {
		return PTR_ERR(smartled.class);
	}

	/* 5、创建设备 */
	smartled.device = device_create(smartled.class, NULL, smartled.devid, NULL, LED_NAME);
	if (IS_ERR(smartled.device)) {
		return PTR_ERR(smartled.device);
	}

	/* 设置LED所使用的GPIO */
	/* 1、获取设备节点：smartled */
	smartled.nd = of_find_node_by_path("/smartled");
	if(smartled.nd == NULL) {
		printk("smartled node not find!\r\n");
		return -EINVAL;
	} else {
		printk("smartled node find!\r\n");
	}

	/* 2、 获取设备树中的gpio属性，得到LED所使用的LED编号 */
	smartled.led_gpio = of_get_named_gpio(smartled.nd, "led-gpio", 0);
	if(smartled.led_gpio < 0) {
		printk("can't get led-gpio");
		return -EINVAL;
	}
	printk("led-gpio num = %d\r\n", smartled.led_gpio);

	/* 3、设置GPIO1_IO03为输出，并且输出高电平，默认关闭LED灯 */

    gpio_request(smartled.led_gpio, "led0");
	ret = gpio_direction_output(smartled.led_gpio, 1);
	if(ret < 0) {
		printk("can't set gpio!\r\n");
	}
	return 0;
}
static int led_remove(struct platform_device *dev)
{
	gpio_set_value(smartled.led_gpio, 1); 
	cdev_del(&smartled.cdev);/*  删除cdev */
	unregister_chrdev_region(smartled.devid, LED_CNT); /* 注销设备号 */

	device_destroy(smartled.class, smartled.devid);
	class_destroy(smartled.class);
	return 0;
}
static const struct of_device_id led_of_match[] = {   /*设备树匹配表*/
    {.compatible = "smart-led"},
    {},                                /*最后一项需为空*/
};
/*platform 驱动结构体*/
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ul-led",                 /*驱动和设备名一致时，相互匹配*/
        .of_match_table = led_of_match,       /*设备树匹配*/
    },
    .probe  =  led_probe,
    .remove =  led_remove,
};




static int __init led_init(void)
{
	return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("destiny");
